package com.briup.jd2407cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.jd2407cms.domain.Slideshow;
import com.briup.jd2407cms.exception.ServiceException;
import com.briup.jd2407cms.result.ResultCode;
import com.briup.jd2407cms.service.SlideshowService;
import com.briup.jd2407cms.mapper.SlideshowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
@Slf4j
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow>
    implements SlideshowService{

    @Autowired
    SlideshowMapper slideshowMapper;

    @Override
    public List<Slideshow> queryAllEnable() {
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Slideshow::getStatus, "启用").orderByDesc(Slideshow::getUploadTime);
        List<Slideshow> lis = slideshowMapper.selectList(lqw);
        if(lis == null || lis.isEmpty()){
            log.error("查询所有启用的轮播图的集合为空");
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        return lis;
    }

    @Override
    public IPage<Slideshow> findAllByPageByCondition(Integer pageNum, Integer pageSize, String desc, String status) {
        // 准备分页
        Page<Slideshow> slideshowPage = new Page<>(pageNum, pageSize);
        // 条件查询
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.like(desc !=null,Slideshow::getDescription,desc)
                .eq(status!=null,Slideshow::getStatus,status);
        return slideshowMapper.selectPage(slideshowPage, lqw);
    }

    @Override
    public void addOrEdit(Slideshow slideshow) {
        // 获取参数里的id
        Integer id = slideshow.getId();
        String url = slideshow.getUrl();
        boolean flag = false;
        // 验证url是否为空
        if(url == null){
            // 抛个异常
            throw new ServiceException(ResultCode.IMG_URL_IS_EMPTY);
        }else {
            // 保证新增的url的唯一性
            LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
            lqw.select(Slideshow::getUrl).eq(Slideshow::getUrl,url);
            List<Slideshow> urls = slideshowMapper.selectList(lqw);
            if(urls != null && !urls.isEmpty()){
                flag = true;
            }
        }
        // 判断
        if(Objects.isNull(id)){
            if(flag){
                throw new ServiceException(ResultCode.DATA_NOT_UNIQUE);
            }
            // 新增
            if(slideshow.getStatus() == null){
                slideshow.setStatus("启用");
            }
            slideshow.setDeleted(0);
            slideshow.setUploadTime(LocalDateTime.now());
            slideshowMapper.insert(slideshow);
        }else {
            // 根据id查询一下已有的对象
            Slideshow oldS = slideshowMapper.selectById(id);
            // 判断更新的url和原来的url是否一样
            if(oldS.getUrl().equals(url)){
                slideshow.setUrl(null);
            }else {
                if(flag){
                    throw new ServiceException(ResultCode.DATA_NOT_UNIQUE);
                }
            }
            // 更新
            slideshowMapper.updateById(slideshow);
        }
    }

    @Override
    public void removeBatch(List<Integer> ids) {
        // 判断ids的数量
        int size = ids.size();
        // 根据ids先查，查出来的对象数量和ids数量是否一致（极端，数据量不匹配，不提供删除）
        List<Slideshow> slideshows = slideshowMapper.selectBatchIds(ids);
        if(slideshows.size() != size){
            throw new ServiceException(ResultCode.DATA_MISMATCH);
        }
        // 优化版本：返回数据丰富一点，提供删除了多少条，几条删除失败，为什么失败，失败的ID是哪些

        if(size == 1){
            removeById(ids.get(0));
        }else {
            removeByIds(ids);
        }
    }
}




